/**
 * 
 */

package com.chengyu.eyc.listener;

import java.util.Date;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chengyu.eyc.enums.OrderStatusEnum;
import com.chengyu.eyc.model.EycOrder;
import com.chengyu.eyc.queue.RabbitMQConfig;
import com.chengyu.eyc.service.EycService;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author kevin
 *
 */
@Slf4j
@Component
public class ProcessOrderQueueListener {

    @Autowired
    EycService eycService;

    @RabbitListener(queues = RabbitMQConfig.ORDER_QUEUE)
    @RabbitHandler
    public void process(EycOrder eycOrder) {
        try {
            log.info("订单：{},支付状态：{}", eycOrder.getOrderNum(),eycOrder.getPayStatus());
            if (StrUtil.equals(eycOrder.getStatus(), OrderStatusEnum.SUBMITED.getCode()) ) {
                log.info("处理订单：{}", eycOrder.getOrderNum());
                eycOrder.setAuditTime(new Date());
                eycOrder.setStatus(OrderStatusEnum.CONFIRM.getCode());
                if (StrUtil.isBlank(eycOrder.getAuditPerson())) {
                    eycOrder.setAuditPerson("system");
                }
                this.eycService.updateEycOrder(eycOrder);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

    }

}
